package top.infopub.mgr.sys.service.impl;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.sys.dao.PurviewDao;
import top.infopub.mgr.sys.service.PurviewService;
import top.infopub.model.sys.Purview;
import top.infopub.security.service.SysPurviewService;
import top.infopub.security.vo.PurviewVO;


@Service
public class PurviewServiceImpl extends GenericServiceImpl<Purview, String> implements PurviewService, SysPurviewService {

    @Autowired
    private PurviewDao purviewDao;

    @Override
    public GenericDao<Purview, String> getDao() {
        return purviewDao;
    }

    @Override
    public List<PurviewVO> queryAll() {
        List<Purview> dbPur = this.purviewDao.selectByEntityWhere(new Purview());
        if (dbPur == null) {
            return null;
        }
        List<PurviewVO> secPurList = new ArrayList<PurviewVO>();
        for (Purview pur : dbPur) {
            PurviewVO secPur = new PurviewVO();
            secPur.setPurviewCode(pur.getSpurCode());
            secPur.setPurviewUrl(pur.getSurlPath());
            secPurList.add(secPur);
        }
        return secPurList;
    }

    @Override
    public List<PurviewVO> queryByUserId(String userId) {
        List<PurviewVO> voArr = new ArrayList<PurviewVO>();
        List<Purview> entity = this.purviewDao.selectByUserId(userId);
        if (entity != null) {
            for (Purview r : entity) {
                PurviewVO vo = new PurviewVO();
                vo.setPurviewCode(r.getSpurCode());
                vo.setPurviewId(r.getId());
                vo.setPurviewUrl(r.getSurlPath());
                voArr.add(vo);
            }
        }
        return voArr;
    }

    @Override
    public boolean isNameExist(Purview param) {
        return purviewDao.isNameExist(param) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String[] checkboxId) {
        for (String id : checkboxId) {
            purviewDao.deleteByPrimaryKey(id);
        }

    }

    @Override
    public List<Purview> selectByUserId(String userId) {
        return purviewDao.selectByUserId(userId);
    }

    @Override
    public List<String> selectByParentId(String sparentId) {
        return purviewDao.selectByParentId(sparentId);
    }
    
}